package webdev.fragment;

import webdev.common.Common;
import webdev.danagtravel.MainActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dev.danang.R;

public class LeftNavigationFragment extends Fragment implements OnClickListener {
	private final static String KEY_ID_USER = "index";
	public static final int TAB_USER_PROFILE = 0;
	public static final int TAB_HOME = 0;
	public static final int TAB_INFOR_OVERVIEW = 1;
	public static final int TAB_INFOR_HISTORY = 2;
	public static final int TAB_INFOR_SYMBOL = 3;
	public static final int TAB_INFOR_PHOTOS = 4;
	public static final int TAB_INFOR_VIDEOS = 5;
	public static final int TAB_TOR_DESTINATION = 6;
	public static final int TAB_TOR_HOTEL = 7;
	public static final int TAB_TOR_TRAVEL = 8;
	public static final int TAB_TOR_CUISINE = 9;
	public static final int TAB_TOR_ENTERTAINMENT = 10;
	public static final int TAB_SET_ABOUT = 11;
	public static final int TAB_SET_HELP = 12;
	public static final int TAB_SET_CHANGE_PASSWORD = 13;
	public static final int TAB_SET_BOOKING = 14;
	
	public int curentTab = TAB_INFOR_OVERVIEW;
	private int imageCircleSize;
	private String ID_USER;

	// info

	private ImageView mImageViewAvatar;

	private RelativeLayout mNavLeftAvatar;
	private RelativeLayout mNavLeftOverview;
	private RelativeLayout mNavLeftHistory;
	private RelativeLayout mNavLeftSysmbol;
	
	private RelativeLayout mNavLeftPhotos;
	private RelativeLayout mNavLeftVideos;
	private RelativeLayout mNavLeftChangePass;
	private RelativeLayout mNavLeftAbout;
	private RelativeLayout mNavLeftLogout;
	
	private RelativeLayout mNavLefDestination;
	private RelativeLayout mNavLeftHotel;
	private RelativeLayout mNavLeftTravel;
	private RelativeLayout mNavLeftCuisine;
	private RelativeLayout mNavLeftEntertainment;
	private RelativeLayout mNavLeftListBooking;

	private TextView mCountNotification;
	private TextView mCountFollow;
	private TextView mCountPhoto;
	private TextView mCountVideo;
	private TextView mCountFriend;
	private TextView mUserName;
	
	private Context mContext;
	
	public LeftNavigationFragment newInstance(String id_user) {
		LeftNavigationFragment f = new LeftNavigationFragment();

		// Supply index input as an argument.
		Bundle args = new Bundle();
		args.putString(KEY_ID_USER, id_user);
		f.setArguments(args);

		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.youlook_left_navigation, null);
		init(layout);
		setEventForMembers();
		ID_USER = getArguments().getString(KEY_ID_USER);
		return layout;
	}

	private void init(View layout) {
		// Get parent activity
        mContext = getActivity();
		
		mImageViewAvatar = (ImageView) layout
				.findViewById(R.id.left_navigation_image_view_avatar);
		mNavLeftAvatar = (RelativeLayout) layout
				.findViewById(R.id.nav_left_avatar);
		mNavLeftOverview = (RelativeLayout) layout
				.findViewById(R.id.layout_nav_overview);
		mNavLeftHistory = (RelativeLayout) layout
				.findViewById(R.id.nav_left_history);
		mNavLeftSysmbol = (RelativeLayout) layout
				.findViewById(R.id.nav_left_symbol);
		
		mNavLeftPhotos = (RelativeLayout) layout
				.findViewById(R.id.nav_left_photos);
		mNavLeftVideos = (RelativeLayout) layout
				.findViewById(R.id.nav_left_videos);
		mNavLeftAbout = (RelativeLayout) layout
				.findViewById(R.id.nav_left_about);
		mNavLeftChangePass = (RelativeLayout) layout
				.findViewById(R.id.nav_left_change_pass);
		mNavLeftLogout = (RelativeLayout) layout
				.findViewById(R.id.nav_left_logout);
		mNavLeftListBooking = (RelativeLayout) layout
				.findViewById(R.id.nav_left_booking);
		
		mNavLefDestination = (RelativeLayout) layout.findViewById(R.id.nav_left_destination);
		mNavLeftHotel = (RelativeLayout) layout.findViewById(R.id.nav_left_hotel);
		mNavLeftTravel = (RelativeLayout) layout.findViewById(R.id.nav_left_travel);
		mNavLeftCuisine = (RelativeLayout) layout.findViewById(R.id.nav_left_cuisine);
		mNavLeftEntertainment = (RelativeLayout) layout.findViewById(R.id.nav_left_entertainment);

		mCountPhoto = (TextView) layout.findViewById(R.id.count_photos);
		mCountVideo = (TextView) layout.findViewById(R.id.count_videos);
		mUserName = (TextView) layout
				.findViewById(R.id.left_navigation_text_view_name_user);
		showFocus(TAB_INFOR_OVERVIEW);
//		mNavLeftOverview
//				.setBackgroundResource(R.color.bg_left_navigation_item_selector);
		imageCircleSize = getActivity().getResources().getDimensionPixelSize(
				R.dimen.image_circle_size_avatar_user);
	}

	private void setEventForMembers() {
		// Set onclick listener layout items left navigation
		mNavLeftAvatar.setOnClickListener(this);
		mNavLeftHistory.setOnClickListener(this);
		mNavLeftOverview.setOnClickListener(this);
		mNavLeftSysmbol.setOnClickListener(this);
		
		
		mNavLeftPhotos.setOnClickListener(this);
		mNavLeftVideos.setOnClickListener(this);
		mNavLeftAbout.setOnClickListener(this);
		mNavLeftChangePass.setOnClickListener(this);
		mNavLeftLogout.setOnClickListener(this);
		
		mNavLefDestination.setOnClickListener(this);
		mNavLeftHotel.setOnClickListener(this);
		mNavLeftTravel.setOnClickListener(this);
		mNavLeftCuisine.setOnClickListener(this);
		mNavLeftEntertainment.setOnClickListener(this);
		mNavLeftListBooking.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.layout_nav_overview:
			if (curentTab != TAB_INFOR_OVERVIEW) {
				((MainActivity) getActivity()).setHeader("TỔNG QUAN");
				((MainActivity) getActivity()).setFragmentContent(
						new InforOverViewFragment().newInstance(InforOverViewFragment.SHOW_OVERVIEW),
						InforOverViewFragment.TAG);
				showFocus(TAB_INFOR_OVERVIEW);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_history:
			if (curentTab != TAB_INFOR_HISTORY) {
				((MainActivity) getActivity()).setHeader("LỊCH SỬ");
				((MainActivity) getActivity()).setFragmentContent(
						new InforOverViewFragment().newInstance(InforOverViewFragment.SHOW_HISTORY),
						InforOverViewFragment.TAG);
				showFocus(TAB_INFOR_HISTORY);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_symbol:
			if (curentTab != TAB_INFOR_SYMBOL) {
				((MainActivity) getActivity()).setHeader("BIỂU TƯỢNG");
				((MainActivity) getActivity()).setFragmentContent(
						new InforOverViewFragment().newInstance(InforOverViewFragment.SHOW_SYMBOL),
						InforOverViewFragment.TAG);
				showFocus(TAB_INFOR_SYMBOL);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
			
		case R.id.nav_left_photos:
			if (curentTab != TAB_INFOR_PHOTOS) {
				((MainActivity) getActivity()).setHeader("PHOTO");
				((MainActivity) getActivity()).setFragmentContent(
						new PhotosFragment(),
						PhotosFragment.TAG);
				showFocus(TAB_INFOR_PHOTOS);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_videos:
			if (curentTab != TAB_INFOR_VIDEOS) {
				((MainActivity) getActivity()).setHeader("VIDEOS");
				((MainActivity) getActivity()).setFragmentContent(
						new VideosFragment(),
						VideosFragment.TAG);
				showFocus(TAB_INFOR_VIDEOS);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_change_pass:
			if (curentTab != TAB_SET_CHANGE_PASSWORD) {
				((MainActivity) getActivity()).setHeader("THAY ĐỔI MẬT KHẨU");
				((MainActivity) getActivity()).setFragmentContent(
						new ChangePasswordFragment(),
						ChangePasswordFragment.TAG);
				showFocus(TAB_SET_CHANGE_PASSWORD);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_about:
			if (curentTab != TAB_SET_ABOUT) {
				((MainActivity) getActivity()).setHeader("THÔNG TIN");
				((MainActivity) getActivity()).setFragmentContent(
						new AboutMeFragment(),
						AboutMeFragment.TAG);
				showFocus(TAB_SET_ABOUT);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_logout:
			dialog().show();
			break;
		default:
			break;
		}

		switch (v.getId()) {
		case R.id.nav_left_destination:
			// set Fragment content
			
			if (curentTab != TAB_TOR_DESTINATION) {
				Common.TAB_ForSearch = 0;
				((MainActivity) getActivity()).setHeader("ĐIỂM ĐẾN");
				((MainActivity)mContext).mSearchNavigationFragment.callReload();
				((MainActivity)mContext).mapFragment = MapPlacesFragment.newInstance(0);
				((MainActivity)mContext).setFragmentContent(((MainActivity)mContext).mapFragment, MapPlacesFragment.TAG);
				
				showFocus(TAB_TOR_DESTINATION);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_hotel:
			if (curentTab != TAB_TOR_HOTEL) {
				Common.TAB_ForSearch = 1;
				((MainActivity) getActivity()).setHeader("KHÁCH SẠN");
				((MainActivity)mContext).mSearchNavigationFragment.callReload();
				// set Fragment content
				((MainActivity)mContext).mapFragment = MapPlacesFragment.newInstance(1);
				((MainActivity)mContext).setFragmentContent(((MainActivity)mContext).mapFragment, MapPlacesFragment.TAG);
				showFocus(TAB_TOR_HOTEL);
				
				
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_travel:
			if (curentTab != TAB_TOR_TRAVEL) {
				Common.TAB_ForSearch = 2;
				((MainActivity) getActivity()).setHeader("LỮ HÀNH");
				((MainActivity)mContext).mSearchNavigationFragment.callReload();
				// set Fragment content
				((MainActivity)mContext).mapFragment = MapPlacesFragment.newInstance(2);
				((MainActivity)mContext).setFragmentContent(((MainActivity)mContext).mapFragment, MapPlacesFragment.TAG);
				showFocus(TAB_TOR_TRAVEL);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_cuisine:
			if (curentTab != TAB_TOR_CUISINE) {
				Common.TAB_ForSearch = 3;
				((MainActivity) getActivity()).setHeader("ẨM THỰC");
				((MainActivity)mContext).mSearchNavigationFragment.callReload();
				// set Fragment content
				((MainActivity)mContext).listFragment = ListAmThucFragment.newInstance();
				((MainActivity)mContext).setFragmentContent(((MainActivity)mContext).listFragment, ListAmThucFragment.TAG);
				showFocus(TAB_TOR_CUISINE);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_entertainment:
			if (curentTab != TAB_TOR_ENTERTAINMENT) {
				Common.TAB_ForSearch = 4;
				((MainActivity) getActivity()).setHeader("GIẢI TRÍ");
				((MainActivity)mContext).mSearchNavigationFragment.callReload();
				// set Fragment content
				((MainActivity)mContext).mapFragment = MapPlacesFragment.newInstance(4);
				((MainActivity)mContext).setFragmentContent(((MainActivity)mContext).mapFragment, MapPlacesFragment.TAG);
				showFocus(TAB_TOR_ENTERTAINMENT);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		case R.id.nav_left_booking:
			// set Fragment content
			
			if (curentTab != TAB_SET_BOOKING) {
				((MainActivity) getActivity()).setHeader("THÔNG TIN ĐẶT VÉ");
				// set Fragment content
				((MainActivity)mContext).setFragmentContent(new ListBookingFragment(), "");
				showFocus(TAB_SET_BOOKING);
			} else {
				((MainActivity) getActivity()).toggle();
			}
			break;
		default:
			break;
		}
	}

	public Dialog dialog() {
		// Use the Builder class for convenient dialog construction
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("Log Out")
				.setMessage("Bạn có chắc chắn thoát?")
				.setPositiveButton("Đồng ý",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								getActivity().finish();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								// User cancelled the dialog
							}
						});

		// Create the AlertDialog object and return it
		return builder.create();

	}

	public void showFocus(int tab) {
		// First unfocus current tab
		setUnfocusLayout();
		// Set current position
		curentTab = tab;
		// Set Focus current tab
		setFocusLayout();
	}

	public void setFocusLayout() {
		switch (curentTab) {
		case TAB_USER_PROFILE:
			mNavLeftAvatar.setSelected(true);
			break;
		case TAB_INFOR_OVERVIEW:
			mNavLeftOverview.setSelected(true);
			break;
		case TAB_INFOR_HISTORY:
			mNavLeftHistory.setSelected(true);
			break;
		case TAB_INFOR_SYMBOL:
			mNavLeftSysmbol.setSelected(true);
			break;
		case TAB_INFOR_PHOTOS:
			mNavLeftPhotos.setSelected(true);
			break;
		case TAB_INFOR_VIDEOS:
			mNavLeftVideos.setSelected(true);
			break;
		case TAB_TOR_DESTINATION:
			mNavLefDestination.setSelected(true);
			break;
		case TAB_TOR_HOTEL:
			mNavLeftHotel.setSelected(true);
			break;
		case TAB_TOR_TRAVEL:
			mNavLeftTravel.setSelected(true);
			break;
		case TAB_TOR_CUISINE:
			mNavLeftCuisine.setSelected(true);
			break;
		case TAB_TOR_ENTERTAINMENT:
			mNavLeftEntertainment.setSelected(true);
			break;
		case TAB_SET_ABOUT:
			mNavLeftAbout.setSelected(true);
			break;
		case TAB_SET_CHANGE_PASSWORD:
			mNavLeftChangePass.setSelected(true);
			break;
		case TAB_SET_BOOKING:
			mNavLeftListBooking.setSelected(true);
			break;
			
		default:
			break;
		}
	}

	public void setUnfocusLayout() {
		switch (curentTab) {
		case TAB_USER_PROFILE:
			mNavLeftAvatar.setSelected(false);
			break;
		case TAB_INFOR_OVERVIEW:
			mNavLeftOverview.setSelected(false);
			break;
		case TAB_INFOR_HISTORY:
			mNavLeftHistory.setSelected(false);
			break;
		case TAB_INFOR_SYMBOL:
			mNavLeftSysmbol.setSelected(false);
			break;
		case TAB_INFOR_PHOTOS:
			mNavLeftPhotos.setSelected(false);
			break;
		case TAB_INFOR_VIDEOS:
			mNavLeftVideos.setSelected(false);
			break;
		case TAB_TOR_DESTINATION:
			mNavLefDestination.setSelected(false);
			break;
		case TAB_TOR_HOTEL:
			mNavLeftHotel.setSelected(false);
			break;
		case TAB_TOR_TRAVEL:
			mNavLeftTravel.setSelected(false);
			break;
		case TAB_TOR_CUISINE:
			mNavLeftCuisine.setSelected(false);
			break;
		case TAB_TOR_ENTERTAINMENT:
			mNavLeftEntertainment.setSelected(false);
			break;
		case TAB_SET_ABOUT:
			mNavLeftAbout.setSelected(false);
			break;
		case TAB_SET_CHANGE_PASSWORD:
			mNavLeftChangePass.setSelected(false);
			break;
		case TAB_SET_BOOKING:
			mNavLeftListBooking.setSelected(false);
			break;
		default:
			break;
		}
	}
}
